import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ShowDaoImpl implements ShowDao {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private MovieDao movieDao = new MovieDaoImpl();
    private HallDao hallDao = new HallDaoImpl();

    @Override
    public boolean addShow(Show show) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO show_info VALUES (?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, show.getShowId());
            pstmt.setString(2, show.getMovie().getMovieId());
            pstmt.setString(3, show.getHall().getHallName());
            pstmt.setString(4, show.getShowTime().format(formatter));
            pstmt.setDouble(5, show.getPrice());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeResources(conn, pstmt, null);
        }
    }

    @Override
    public List<Show> getAllShows() {
        List<Show> shows = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM show_info ORDER BY show_time";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                String showId = rs.getString("show_id");
                String movieId = rs.getString("movie_id");
                String hallName = rs.getString("hall_name");
                LocalDateTime showTime = LocalDateTime.parse(rs.getString("show_time"), formatter);
                double price = rs.getDouble("price");

                Movie movie = movieDao.getMovieById(movieId);
                Hall hall = hallDao.getHallByName(hallName);

                if (movie != null && hall != null) {
                    shows.add(new Show(showId, movie, hall, showTime, price));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(conn, pstmt, rs);
        }
        return shows;
    }

    @Override
    public Show getShowById(String showId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM show_info WHERE show_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, showId);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                String movieId = rs.getString("movie_id");
                String hallName = rs.getString("hall_name");
                LocalDateTime showTime = LocalDateTime.parse(rs.getString("show_time"), formatter);
                double price = rs.getDouble("price");

                Movie movie = movieDao.getMovieById(movieId);
                Hall hall = hallDao.getHallByName(hallName);

                if (movie != null && hall != null) {
                    return new Show(showId, movie, hall, showTime, price);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(conn, pstmt, rs);
        }
        return null;
    }
}
    